public class OpportunitySolutionNoFSetsExtController {    
    public List<SolutionComponent__c> components{get;set;}
    public List<string> componentFields{get;set;}
    public List<string> solutionFields{get;set;}
    
    public Solution__c soln {get; set;}
    
    public Boolean IsTechnology{get;set;}
    public Boolean IsConsulting{get;set;}
    public Boolean IsOutsourcing{get;set;}
    
    public Boolean disableInput{get; set;}
    public string toDelete{ get; set; }
    public string opp{get;set;}
    public Boolean isDummy{get; set;}
    
    public String compIdx{get;set;}
    
    public String SolutionComponentsTitle{get; set;}
    
    public String RecType{get;set;}
    private ApexPages.StandardController solnController;
    

    private List<string> solFieldsToActivate;
    
    
    private void setAllSolFieldsActive(){
        
        solFieldsToActivate = new List<string>();         
        Schema.DescribeSObjectResult solobj = Solution__c.SObjectType.getDescribe(); 
        Map<String, Schema.SObjectField> allFieldsMap = solobj.fields.getMap();

        for(String fldName : allFieldsMap.keySet()){
            Schema.SObjectField fld = allFieldsMap.get(fldName);
            DescribeFieldResult fieldResult = fld.getDescribe();
            if(fieldResult.isCustom()){
                
                    string fieldName = fieldResult.getName();
                        solFieldsToActivate.add(fieldName);
            }
        }        
    }
    
        private void setSolutionFields(){
        solutionFields = new List<string>();
        componentFields = new List<string>();
        
        solutionFields.add('product__c');
        solutionFields.add('opportunity__c'); 
        
        componentFields.add('Component__c');
         
                
        if(IsTechnology){
            solutionFields.add('deployment__c');
            solutionFields.add('purchase_rental__c');               
            solutionFields.add('rental_value__c');
            solutionFields.add('rental_value_pa_margin__c');               
            solutionFields.add('maintenance_fee__c');
            solutionFields.add('maintenance_fee_margin__c');               
            solutionFields.add('license_fee__c');
            solutionFields.add('license_fee_margin__c');
            solutionFields.add('initial_fee__c');
            solutionFields.add('initial_fee_margin__c'); 
            solutionFields.add('tcv_technology__c');
            solutionFields.add('tcv_technology_margin__c');
                
            
            componentFields.add('Implementation_Start_Date__c');
            componentFields.add('Go_Live_Date__c');
            componentFields.add('Indicative_Value__c');                     
        }
        else if (IsConsulting){
                
           SolutionComponentsTitle = 'Modules';
           solutionFields.add('tcv_consulting__c');
           solutionFields.add('tcv_consulting_margin__c');
                
           SolutionComponentsTitle = 'Services';
           componentFields.add('Days__c');
           componentFields.add('Implementation_Start_Date__c');
           componentFields.add('Go_Live_Date__c'); 
           
           componentFields.add('Total_Implementation_Revenue__c'); 
           componentFields.add('Total_Implementation_Cost__c');
           componentFields.add('Implementation_Type__c');
        }
        else if (IsOutsourcing){
            solutionFields.add('deployment__c');
            solutionFields.add('tcv_outsourcing__c');
            solutionFields.add('tcv_outsourcing_margin__c'); 
                
            SolutionComponentsTitle = 'Countries';
            componentFields.add('Towers__c');         
            componentFields.add('FM_Payroll_Revenue__c');
            componentFields.add('HRO_Revenue__c');
            componentFields.add('Hosting_Amount__c'); 
            componentFields.add('Outsourcing_Margin__c');
            componentFields.add('Implementation_Start_Date__c');
            componentFields.add('Go_Live_Date__c'); 
            componentFields.add('Employees_per_country__c');
            componentFields.add('HR_Heads_in_Country__c');
            componentFields.add('HR_Heads_Offshore__c');
            componentFields.add('Payroll_Heads_in_Country__c');
            componentFields.add('Payroll_Heads_Offshore__c');
            componentFields.add('PEPM__c');
               
            componentFields.add('Tier_1_Service_Hub__c');                         
            componentFields.add('Hosted_By__c');
            
            componentFields.add('Implemented_By__c');
        }        
        
    }
              
    public OpportunitySolutionNoFSetsExtController(ApexPages.StandardController stdController) {   
      
        setAllSolFieldsActive();
        stdController.addFields(solFieldsToActivate);
        
        
        solnController = stdController;
        soln = (Solution__c)stdController.getRecord();
    }
        
    public void Initialise(){        
        components = new List<SolutionComponent__c>();
        Schema.DescribeSObjectResult solcomp = SolutionComponent__c.SObjectType.getDescribe(); 
        Map<String, Schema.SObjectField> allFieldsMap = solcomp.fields.getMap();

        String SOQLSelect = '';
        for(String fldName : allFieldsMap.keySet()){
            Schema.SObjectField fld = allFieldsMap.get(fldName);
            DescribeFieldResult fieldResult = fld.getDescribe();
            string fieldName = fieldResult.getName();
            System.debug('Got field:'+fieldName);
            if(SOQLSelect != ''){
                SOQLSelect += ',' + fieldName;
            }
            else{
                SOQLSelect += 'select ' + fieldName;
            }     
        }               
        if(soln.id !=null){
            String SOQLStmt =SOQLSelect + ' from SolutionComponent__c where Solution__r.id =' + '\'' + soln.id + '\'';        
            components = Database.query(SOQLStmt);
        }
                     
        Schema.DescribeSObjectResult R = Solution__c.SObjectType.getDescribe();
        Map<ID,Schema.RecordTypeInfo> recTypesMap = R.getRecordTypeInfosByID();
        Schema.RecordTypeInfo recTypeInst = recTypesMap.get(soln.RecordTypeId);         
        RecType = recTypeInst.getName();     
        
        IsTechnology = (RecType == 'HR Technology');
        IsConsulting = (RecType == 'HR Consulting');
        IsOutsourcing = (RecType == 'HR Outsourcing');
        
        setSolutionFields();
       
        techodisable();
        oppchange();
        hideDummyOpty();

    }
    
    Public void currencyDefault(){
        system.debug('solution id ' + soln.id);
        if(soln.id != null){
        Solution__c sln = [select CurrencyIsoCode,Opportunity__r.id from Solution__c where id =: soln.id]; 
        opportunity opty = [select CurrencyIsoCode from opportunity where id =: sln.Opportunity__r.id];
        sln.currencyIsoCode = opty.CurrencyIsoCode;        
        system.debug('solution id ++++  ' + soln.id);
        }    
    }
    
    public PageReference cancel(){
      
        PageReference p;
        p =  solnController.cancel();
        try{
            soln = (Solution__c)solnController.getRecord(); 
            Solution__c sol=[select id,Opportunity__r.id,Account__c from Solution__c where id=:soln.Id];
            DummyOptyHolder__c optyCustomSetting = DummyOptyHolder__c.getInstance();
            string DumyOptyId = optyCustomSetting.OptyId__c;

              if(sol.Opportunity__r.id==DumyOptyId)
                {
                 p=new PageReference('/'+sol.Account__c);
                }
                else
                {
                  p=new PageReference('/'+sol.Opportunity__r.id);
                }
        }
        catch(Exception e){
            System.debug('******* exception' + e);        
        }
        return p;
    }
    
    public void NewSolutionComponent(){
       
        SolutionComponent__c solComp = new SolutionComponent__c();
        solComp.ParentType__c = soln.product__c;
        solComp.Solution__c = null;
        components.add(solComp);
        techodisable();
        
    }
        
    public PageReference save() {
        Savepoint sp = Database.setSavepoint();
        PageReference retPage = null;
        try {
        
            retPage =  solnController.save();
           
            soln = (Solution__c)solnController.getRecord(); 
            
            Solution__c sol=[select id,Opportunity__r.id,Account__c from Solution__c where id=:soln.Id];
            System.Debug('ACCOUNTDIDIDIDI:'+sol.Account__c);
             
             retPage=new PageReference('/'+sol.Opportunity__r.id);
             
                DummyOptyHolder__c optyCustomSetting = DummyOptyHolder__c.getInstance();
                string DumyOptyId = optyCustomSetting.OptyId__c;

                
            for (SolutionComponent__c solComp : components){
                if(solComp.Solution__c == null)
                {
                    solComp.Solution__c = soln.Id;                   
                }
                currencyDefault();  
                solComp.CurrencyIsoCode = soln.CurrencyIsoCode;
                system.debug('solution component currencycode' + solComp.CurrencyIsoCode);
                Database.upsert(solComp);
                }
                if(sol.Opportunity__r.id==DumyOptyId)
                {
                 retPage=new PageReference('/'+sol.Account__c);
                }
                else if(sol.Opportunity__c==null)
                {
                 sol.Opportunity__c=DumyOptyId;
                 update sol;
                 retPage=new PageReference('/'+sol.Account__c);
                }
            return retPage;                                
        }
        catch (Exception e) {
            Database.rollback(sp);
            System.debug('******** exception ' + e);
        }
        return null;
        
    }
    
 
    
    public void deleteSectionItem() {
        SolutionComponent__c solComp;
        System.debug('Component Index Reference' + compIdx);
        String[] columnparts = compIdx.split(':');
        
        Integer idx =0;
        Integer tableRow;
        
        for(String tkn : columnparts){ 
                
                if(tkn == 'DeleteColumn'){
                        tableRow = Integer.valueOf(columnparts[idx-1]);
                        System.debug('Table Row Is: ' +tableRow );
                }
                idx++;          
        }
        
        System.Debug('todelete'+todelete);
        if(this.toDelete != '' || this.toDelete != null){
            try{                            
                                                        List<SolutionComponent__c> modcomponents = new List<SolutionComponent__c>();
                idx =0;
                for(SolutionComponent__c comp: components){
                                        if(idx != tableRow){
                                                 modcomponents.add(comp);
                                        }
                                        idx++;
                }
                components = modcomponents;
                solComp = [select id from SolutionComponent__c where id = : this.toDelete];                   
                delete solComp;
                
                              
            }
            catch(Exception e){
                System.Debug('Error while deletion:'+e);
            }
            techodisable();
                        

        }        
    }
   public void oppchange()
    {
     soln = (Solution__c)solnController.getRecord();
     
     if(soln.Opportunity__c==null)
     {
      
      DummyOptyHolder__c optyCustomSetting = DummyOptyHolder__c.getInstance();
      string DumyOptyId = optyCustomSetting.OptyId__c;
      soln.Opportunity__c=DumyOptyId ;
       
     }
    }
  
    public void techodisable(){

        if(components.size() > 0){
            disableInput = true;
           
        }
        else{
            disableInput = false;
            
        }
    }
    public void hideDummyOpty(){
     soln = (Solution__c)solnController.getRecord();
     System.Debug('kalpesh...solutionId===:-'+soln .id);
    
     if(soln.id!=null)
     {
      DummyOptyHolder__c optyCustomSetting = DummyOptyHolder__c.getInstance();
      string DumyOptyId = optyCustomSetting.OptyId__c;
      Solution__c sln = [select id,Opportunity__r.id,Account__c from Solution__c where id =:soln.id];
      if(sln.opportunity__r.id==DumyOptyId){
      System.Debug('kalpesh...dummy===:-'+sln.opportunity__r.id);
       isDummy = false;
      }
      else{
      System.Debug('kalpesh...dummy===else:-'+sln.opportunity__r.id);
      isDummy = true;
      }
      }
      
      
      }
}